Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
¿Cómo dividir un texto al detectar la cadena "\ n" y un espacio?

por ejemplo

tengo una cadena como

 String example = "Hello\nHow\nAre\nyou today? I Love Pizza"; //

Lo que quiero es una matriz como esta

 [Hello, \n, How, \n, Are, \n, you, today? , I , Love, Pizza]

Ya probé

 String[] splited = example.split("[\\n\\s]+");// as will a lot of regular exprisions like ("\\n\\r+") etc.

pero no funcionaron.

¿alguien tiene una solución por favor?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede dividir afirmando una secuencia de nueva línea \R a la izquierda o a la derecha, o hacer coincidir un carácter de espacio en blanco horizontal \h usando una alternancia |

 (?=\\R)|(?<=\\R)|\\h

demostración de Java

Por ejemplo

 String example = "Hello\nHow\nAre\nyou today? I Love Pizza"; // String[] splited = example.split("(?=\\R)|(?<=\\R)|\\h"); for (String element : splited) { if (element.equals("\n")) element = "newline"; System.out.println(element); }

Producción

 Hello newline How newline Are newline you today? I Love Pizza
over 4 years ago · Santiago Trujillo Report

0

// Split on the following: // look ahead for '\n' which is preceded by a character that is not '\n' // OR look ahead for a character that is not '\n' preceded by '\n' // OR a single space. String regex = "(?=\\n)(?<!\\n)|(?!\\n)(?<=\\n)| "; String example = "Hello\nHow\nAre\nyou today? I Love Pizza"; // This is the array that you want. String[] splited = example.split(regex); // This is just to display the contents of 'splited'. int count = 0; for (String part : splited) { count++; if (part.equals("\n")) { // Rather than print the actual newline, print its escape sequence System.out.printf("%2d. \\n%n", count); } else { System.out.printf("%2d. %s%n", count, part); } }

Resultado:

 1. Hello 2. \n 3. How 4. \n 5. Are 6. \n 7. you 8. today? 9. I 10. Love 11. Pizza
over 4 years ago · Santiago Trujillo Report

0

Puede hacerlo simplemente usando lookbehind (especificado por ?<= ) o lookahead (especificado por ?= ) para \n y alternado con \s+ (para espacios en blanco).

Manifestación:

 import java.util.Arrays; public class Main { public static void main(String[] args) { String example = "Hello\nHow\nAre\nyou today? I Love Pizza"; // String[] splited = example.split("(?<=\\n)|(?=\\n)|\\s+"); System.out.println(Arrays.toString(splited)); } }

Producción:

 [Hello, , How, , Are, , you, today?, I, Love, Pizza]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!